const createError = require("http-errors")
const express = require("express")
const path = require("path")
const logger = require("morgan")
const dotenv = require("dotenv")
const cors = require("cors")
const responseMiddleware = require("./middlewares/responseMiddleware")
const errorHandler = require("./middlewares/errorHandler")

const indexRouter = require("./routes/index")
const usersRouter = require("./routes/user")
const tableRouter = require("./routes/table")
const app = express()

app.use(logger("dev"))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(express.static(path.join(__dirname, "public")))
app.use(responseMiddleware)
app.use(cors())

const baseURL = "/vue-admin-template"
app.use("/", indexRouter)
app.use(baseURL + "/user", usersRouter)
app.use(baseURL + "/table", tableRouter)

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404))
})

// error handler
app.use(errorHandler)

app.listen(8080, () => {
  console.log("=======================================")
  console.log("服务器成功运行：http://localhost:8080/")
  console.log("=======================================")
})
